#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/14 12:52
# @Author  : leon yan
# @Site    :
# @File    : Parameter.py
# @Software: PyCharm

from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMessageBox)

import Globalist as gl
from SystemSet import QSystemSet
from SheetTBL import QSheetTBL
from Plc import QPlc
from resource.Parameter_ui import Ui_QWFormTable


class QParameter(QMainWindow, Ui_QWFormTable):
    LoginIsOut = pyqtSignal()

    def __init__(self, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.setupUi(self)
        # gl.init() # 调试用
        self._formatTab()

    def __del__(self):  # 析构函数
        print("Parameter is deleted")

    def closeEvent(self, event):
        pass

    def _formatTab(self):
        self.dic_tab = {}
        self.tabWidget.setVisible(False)
        self.tabWidget.clear()  # 清除所有页面
        self.tabWidget.setTabsClosable(True)  # Page有关闭按钮
        self.tabWidget.setDocumentMode(True)
        self.on_actParameter_triggered()

    @pyqtSlot()
    def on_actParameter_triggered(self):
        self._showBtnInfo(self.sender())
        if "参数" in self.dic_tab:
            self.tabWidget.setCurrentIndex(self.dic_tab["参数"])
            self.tabWidget.setVisible(True)
            return
        else:
            formDoc = QSystemSet(self)
            formDoc.setAttribute(Qt.WA_DeleteOnClose)  # 关闭时自动删除
            title = "参数"
            curIndex = self.tabWidget.addTab(formDoc, title)  # 添加到tabWidget
            self.tabWidget.setCurrentIndex(curIndex)
            self.tabWidget.setVisible(True)
            self.dic_tab["参数"] = curIndex

    @pyqtSlot()
    def on_action_Device_triggered(self):
        self._showBtnInfo(self.sender())
        if "设备" in self.dic_tab:
            self.tabWidget.setCurrentIndex(self.dic_tab["设备"])
            self.tabWidget.setVisible(True)
            return
        else:
            formDoc = QSheetTBL("Device")
            formDoc.setAttribute(Qt.WA_DeleteOnClose)  # 关闭时自动删除
            title = "设备"
            curIndex = self.tabWidget.addTab(formDoc, title)  # 添加到tabWidget
            self.tabWidget.setCurrentIndex(curIndex)
            self.tabWidget.setVisible(True)
            self.dic_tab["设备"] = curIndex

    @pyqtSlot()
    def on_action_Plc_Monitor_triggered(self):
        self._showBtnInfo(self.sender())
        if "Plc监控" in self.dic_tab:
            self.tabWidget.setCurrentIndex(self.dic_tab["Plc监控"])
            self.tabWidget.setVisible(True)
            return
        else:
            formDoc = QPlc()
            formDoc.setAttribute(Qt.WA_DeleteOnClose)  # 关闭时自动删除
            title = "Plc监控"
            curIndex = self.tabWidget.addTab(formDoc, title)  # 添加到tabWidget
            self.tabWidget.setCurrentIndex(curIndex)
            self.tabWidget.setVisible(True)
            self.dic_tab["Plc监控"] = curIndex

    @pyqtSlot()
    def on_actUser_triggered(self):
        self._showBtnInfo(self.sender())
        if "用户管理" in self.dic_tab:
            self.tabWidget.setCurrentIndex(self.dic_tab["用户管理"])
            self.tabWidget.setVisible(True)
            return
        formDoc = QSheetTBL("UserTable")
        formDoc.setAttribute(Qt.WA_DeleteOnClose)  # 关闭时自动删除
        title = "用户管理"
        curIndex = self.tabWidget.addTab(formDoc, title)  # 添加到tabWidget
        self.tabWidget.setCurrentIndex(curIndex)
        self.tabWidget.setVisible(True)
        self.dic_tab["用户管理"] = curIndex

    @pyqtSlot()
    def on_actLogOut_triggered(self):
        self._showBtnInfo(self.sender())
        gl.global_var["userVar"]["Admin"] = 0
        gl.global_var["userVar"]["LoginUser"] = ""
        gl.global_var["userVar"]["LoginUserName"] = ""
        gl.global_var["userVar"]["LoginStatus"] = "未登入"
        self.LoginIsOut.emit()
        QMessageBox.information(self, "提示", "注销成功")

    def _showBtnInfo(self, btn):
        pass

    # 以下是Tab的相关控制
    def on_tabWidget_currentChanged(self, index):  # tabWidget当前页面变化
        # print(self.tabWidget.tabText(index))
        self.dic_tab.clear()
        cnt = self.tabWidget.count() > 0  # 再无页面时
        self.tabWidget.setVisible(cnt)
        if cnt != 0:
            i = 0
            n = self.tabWidget.count()
            for i in range(n):
                self.dic_tab[self.tabWidget.tabText(i)] = i
                i += 1

    def on_tabWidget_tabCloseRequested(self, index):  # 分页关闭时关闭窗体
        self.dic_tab.clear()
        if index < 0:
            return
        aForm = self.tabWidget.widget(index)
        aForm.close()


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    objWindow = QParameter()

    objWindow.show()
    sys.exit(app.exec_())
